package com.example.woke;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.http.HttpMethods;
import com.zhongcai.online.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CreditypeActivity extends Activity implements OnItemClickListener {

	private ListView mListView;
	private ArrayList<String> list = new ArrayList<String>();
	private ArrayList<String> list5;
	private ArrayList<String> list4;
	private ArrayList<String> list3;
	private ArrayList<String> list2;
	private ArrayList<String> list1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creditype);
		
		
		mListView = (ListView) findViewById(R.id.credit_type_listview);
		mListView.setOnItemClickListener(this);
		
		findViewById(R.id.avcredit_type_back).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
//		list.add("����");list.add("��ѧ");list.add("����");
//		list.add("���");list.add("Ӧ��");
//		if(list.size() > 0){
//			Myadter adapter = new Myadter();
//			mListView.setAdapter(adapter);
//		}
		getypedata();
	}
	
	class Myadter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int i) {
			// TODO Auto-generated method stub
			return i;
		}

		@Override
		public long getItemId(int i) {
			// TODO Auto-generated method stub
			return i;
		}

		@Override
		public View getView(int i, View convertView, ViewGroup viewgroup) {
			convertView = getLayoutInflater().inflate(R.layout.item_adter_singletext, null);
			TextView mTView = (TextView) convertView.findViewById(R.id.item_single_text);
			
//			if(null != list.get(position) || !list.get(position).equals("")){
//				ImageCache imageCache = mImageLoader.getImageCache();
//				Bitmap bitmap = imageCache.getBitmapFromDiskCache(list.get(position));
//				mIicon.setImageBitmap(bitmap);
//			}
			
//			mImageLoader.loadImage("http://"+list.get(position), mIicon, R.drawable.loading_mapbg);
			mTView.setText(list.get(i));
			return convertView;
		}
		
	}

	private void getypedata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(CreditypeActivity.this);    
		ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "quota");
		params.put("a", "getquotatype");
//		params.put("type", typedd);
//		if(null != application.getuser_id() ){
//			params.put("user_id", application.getuser_id());
//		}
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){


			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("&&&", "&&&"+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						JSONObject object = jsonObject.getJSONObject("data");
//						for (int i = 0; i < array.length(); i++) {
//							JSONObject object = array.getJSONObject(i);
							String str1 = object.getString("p5");
							String str2 = object.getString("p4");
							String str3 = object.getString("p3");
							String str4 = object.getString("p2");
							String str5 = object.getString("p1");
							String h5 = object.getString("h5");
							String h4 = object.getString("h4");
							String h3 = object.getString("h3");
							String h2 = object.getString("h2");
							String h1 = object.getString("h1");
							String[] split5 = h5.split(",");
							String[] split4 = h4.split(",");
							String[] split3 = h3.split(",");
							String[] split2 = h2.split(",");
							String[] split1 = h1.split(",");
							list5 = new ArrayList<String>();
							for (int i = 0; i < split5.length; i++) {
								String string = split5[i];
								list5.add(string);
							}
							list4 = new ArrayList<String>();
							for (int i = 0; i < split4.length; i++) {
								String string = split4[i];
								list4.add(string);
							}
							list3 = new ArrayList<String>();
							for (int i = 0; i < split3.length; i++) {
								String string = split3[i];
								list3.add(string);
							}
							list2 = new ArrayList<String>();
							for (int i = 0; i < split2.length; i++) {
								String string = split2[i];
								list2.add(string);
							}
							list1 = new ArrayList<String>();
							for (int i = 0; i < split1.length; i++) {
								String string = split1[i];
								list1.add(string);
							}
							list.add(str1);list.add(str2);list.add(str3);
							list.add(str4);
							list.add(str5);
//						}
					}else{
						Toast.makeText(CreditypeActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
//				list.add("����");list.add("��ѧ");list.add("����");
//				list.add("���");list.add("Ӧ��");
				if(list.size() > 0){
					Myadter adapter = new Myadter();
					mListView.setAdapter(adapter);
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(CreditypeActivity.this, "数据获取失败！", Toast.LENGTH_SHORT).show();
//				Log.e("response", "ʧ��"+responseString);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
//				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
//				mBar.clearAnimation();
//				mBar.setVisibility(View.GONE);
				Log.e("response", "完成");
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
		Intent intent = new Intent();
		intent.putExtra("list", list.get(i));
		if(i==0){
			intent.putExtra("maplist", list5);
		}else if(i == 1){
			intent.putExtra("maplist", list4);
		}else if(i == 2){
			intent.putExtra("maplist", list3);
		}else if(i == 3){
			intent.putExtra("maplist", list2);
		}else if(i == 4){
			intent.putExtra("maplist", list1);
		}
		setResult(302, intent);
		finish();
	}
}
